<?php
/**
 * 検索　パンくず編集モデルクラス<br />
 *
 * @package apamanshop
 * @author imamura-h@systemsoft.co.jp
 * @copyright コピーライト(要確認)
 */
class Search_Model_Breadcrumb extends Search_Model_Base {

	const SRH_BREAD_KBN = "SRH_";
	protected $_model_common = null;
	public    $_l_breadcrumb = null;

	protected $_baseurl    = "";
	protected $_SrhKbn     = "";
	protected $_controller = "";
	protected $_controller_full = "";
	protected $_pattern    = "";

	protected $_pre_breadcrumb_str = "";
	protected $_single = false;

	public function __construct($breadcrumb=null)
	{
		$this->_model_common = new Search_Model_Common();
		if (is_null($breadcrumb)) {
			$this->_l_breadcrumb = new Apm_Breadcrumb();
		} else {
			$this->_l_breadcrumb = $breadcrumb;
		}
		
		$configObject = Zend_Registry::get('config');
		$config = $configObject->apm->toArray();
		$this->_baseurl = $config["baseurl"];
	}

	/*
	 *  現在地のパンくずを編集
	 */
	public function _getCurrentBreadcrumb($params, $data, $controller)
	{
		// 検索導線を取得
		$this->_controller_full = $controller;
		$this->_controller = strstr($this->_controller_full, '/', true);
		if (!($this->_controller)) $this->_controller = $this->_controller_full;
		$this->_SrhKbn     = $params['SrhKbn'];
		if (empty($this->_SrhKbn)) {
			$this->_SrhKbn = $this->getSrhKbn();
		}

		// 検索導線・パラメータから、編集パターンを取得
		$this->_pattern = $this->getPattern($params);

		// パターンがNull以外で、検索導線パンくずが存在しない場合（グローバグナビ遷移した時など）
		if ($this->_pattern != "NULL" && $this->_l_breadcrumb->getCountPankuzu() <= 1) {
			// 検索導線パンくずを生成する
			$pattern = $this->_pattern;
			$this->_pattern = "NULL";
			$pankuzu = $this->getBreadcrumb($params, $data);
			// パンくず追加
			$this->setBreadcrumb($pankuzu['title'], $pankuzu['url'], $pankuzu['key']);
			// パターンを元に戻す
			$this->_pattern = $pattern;
		}

		// 編集パターン毎にパンくずを生成
		$pankuzu = $this->getBreadcrumb($params, $data);

		if (!empty($pankuzu)) {
			//// 既存のパンくずを編集
			//$this->preUpdBeforeBreadcrumb($params);
			//// 前のパンくずからの引き継ぎパラメータを編集
			//if ($pankuzu['key'] != self::SRH_BREAD_KBN . $this->_SrhKbn) {
			//	$pankuzu['title'] .= $this->_pre_breadcrumb_str;
			//}
			// パンくず追加
			$this->setBreadcrumb($pankuzu['title'], $pankuzu['url'], $pankuzu['key']);
			// 末尾追加
			$this->postBreadcrumb($params);
		}

		return;
	}

	private function setBreadcrumb($title, $url, $key)
	{
		if ($title != "" && $url != "" && $key != "") {
			$this->_l_breadcrumb->add($title, $url, $key);
			//echo "<BR>【パンくず追加】title=".$title.":url=".$url.":key=".$key."<BR>";
		}
		else {
			//echo "<BR>【パンくず追加エラー】title=".$title.":url=".$url.":key=".$key."<BR>";
		}
	}

	/*
	 *  既存のパンくずを編集（現在使用していない）
	 */
	public function preUpdBeforeBreadcrumb($params)
	{
		$count = $this->_l_breadcrumb->getCountPankuzu();
		for ($i=0; $i<$count; $i++)
		{
			// パンくずを取得
			$bread = $this->_l_breadcrumb->getDataByIndex($i);
			$title = $bread['title'];
			$url   = $bread['url'];
			$key   = $bread['key'];
	
			// KEYが建物種別orこだわり
			if (   $key == Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'TYPE'
				|| $key == Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'KODAWARI'
				|| $key == Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'TYPE_KODAWARI')
			{
				// 取り出し
				$this->_pre_breadcrumb_str = " " . $title;
				// パンくず削除
				$this->_l_breadcrumb->deleteByIndex($i);
			}
			// TITLEに全角空白が存在する
			else if ($pos = mb_strpos($title, " "))
			{
				// 切り出し
				$this->_pre_breadcrumb_str = mb_substr($title, $pos);
				$title = mb_substr($title, 0, $pos);
				// パンくずを編集
				$bread['title'] = $title;
				$this->_l_breadcrumb->setDataByIndex($i, $bread);
			}
		}

		// パラメータに建物種別CD・こだわりIDが存在する場合
		if (empty($this->_pre_breadcrumb_str))
		{
			// 検索導線によって、処理をスルー
			if (   $this->_SrhKbn != Apm_Config_Search::SRHKBN_TUKINTUGAKU
				&& $this->_SrhKbn != Apm_Config_Search::SRHKBN_FREEWORD
				&& $this->_SrhKbn != Apm_Config_Search::SRHKBN_YACHINSOUBA
				&& $this->_SrhKbn != Apm_Config_Search::SRHKBN_GAKKONAME
				&& $this->_SrhKbn != Apm_Config_Search::SRHKBN_BUKENREQUEST
				&& $this->_SrhKbn != Apm_Config_Search::SRHKBN_BUKENREQUEST_SYATAKU )
			{
				$this->_pre_breadcrumb_str = "";
				// こだわりID⇒名称
				if (!empty($params['KodawariId'])) {
					$this->_pre_breadcrumb_str .= " " . Apm_Config_Search::$KODAWARI_ID_STR_LIST[$params['KodawariId']];
				}
				// 建物種別CD⇒名称
				if (!empty($params['TatemonoTypeCd'])) {
					$this->_pre_breadcrumb_str .= " " . Apm_Config_Search::$KENSAKU_TATEMONO_SYUBETU['syubetu'.$params['TatemonoTypeCd']];
				} else {
					$this->_pre_breadcrumb_str .= " 賃貸";
				}
			}
		}

		return;
	}

	/*
	 *  パンくず追加要素
	 */
	public function postBreadcrumb($params)
	{
		// 検索導線によって、処理をスルー
		if (   $this->_SrhKbn == Apm_Config_Search::SRHKBN_JYUSYO
			|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_ENSENEKI
			|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_JYUKYOKEITAI
			|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_KODAWARI
			|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_CAMPAIGN)
		{
			$kodawariStr = "";
			$typeStr     = "";
			// こだわりID⇒名称
			if (!empty($params['KodawariId'])) {
				$kodawariStr = Apm_Config_Search::$KODAWARI_ID_STR_LIST[$params['KodawariId']];
			}
			// 建物種別CD⇒名称
			if (!empty($params['TatemonoTypeCd'])) {
				$typeStr = str_replace('賃貸', '', Apm_Config_Search::$KENSAKU_TATEMONO_SYUBETU['syubetu'.$params['TatemonoTypeCd']]);
			}
			
			// 文字列生成
			$tail = "";
			if (!empty($kodawariStr) && !empty($typeStr)) {
				$tail = "から".$kodawariStr."の".$typeStr."を探す";
			}
			else if (!empty($kodawariStr)) {
				$tail = "から".$kodawariStr."の賃貸を探す";
			}
			else if (!empty($typeStr)) {
				$tail = $typeStr."を探す";
			}
			
			// 末尾追加
			if (!empty($tail) && empty($kodawariStr)) {
				// 2014.06.23 パンくずリスト改善
				if ($this->_single) {
					// 最後のパンくず取得
					$bread = $this->_l_breadcrumb->getDataByIndex($this->_l_breadcrumb->getCountPankuzu()-1);
					$title = $bread['title']."から".$tail;
					$url   = "";
					$key   = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'TYPE';
					$this->_l_breadcrumb->addTailArr($title, $url, $key);
				} else {
					$title = $tail;
					$url   = "";
					$key   = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'TYPE';
					$this->_l_breadcrumb->addTailArr($title, $url, $key);
				}
			}
			else if (!empty($tail)) {
				if ($this->_single) $this->_l_breadcrumb->addTailStr($tail);
			}
			else if ($this->_SrhKbn == Apm_Config_Search::SRHKBN_JYUSYO && $this->_pattern == 'PREF') {
				$title = "地域から賃貸を探す";
				$url   = "";
				$key   = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'CHIIKI';
				$this->_l_breadcrumb->addTailArr($title, $url, $key);
			}
			else if ($this->_SrhKbn == Apm_Config_Search::SRHKBN_ENSENEKI && $this->_pattern == 'PREF') {
				$title = "沿線から賃貸を探す";
				$url   = "";
				$key   = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'CHIIKI';
				$this->_l_breadcrumb->addTailArr($title, $url, $key);
			}
			else if ($this->_pattern == 'RAIL') {
				$tail = "の駅から賃貸を探す";
				if ($this->_single) $this->_l_breadcrumb->addTailStr($tail);
			}
			else if ($this->_pattern == 'PREF' || $this->_pattern == 'SITEI' || $this->_pattern == 'CITY' || $this->_pattern == 'TOWN' || $this->_pattern == 'STCD') {
				$tail = "から賃貸を探す";
				if ($this->_single) $this->_l_breadcrumb->addTailStr($tail);
			}
		}
	}

	/*
	 *  パンくずを生成
	 */
	private function getBreadcrumb($params, &$data)
	{
		$title  = "";
		$url    = "";
		$key    = "";
		$pankuzu = array();

		// パラメータデータを取得
		if (empty($data) && $this->_pattern != "NULL") {
			$_model_research = new Search_Model_ResearchPanel();
			$data = $_model_research->convertResarchPanel($this->_SrhKbn, $params);
		}

		// パターンによって処理を分岐
		switch ($this->_pattern)
		{
			// 導線名取得
			case "NULL":
				switch ($this->_SrhKbn)
				{
					case Apm_Config_Search::SRHKBN_JYUSYO:
						$title = "住所から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_ENSENEKI:
						$title = "沿線・駅から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_CHIZU:
						$title = "地図から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_ROSENZU:
						$title = "路線図から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_TUKINTUGAKU:
						$title = "通勤・通学時間から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_SUMITAIEKI:
						$title = "住みたい駅名から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_FREEWORD:
						//$title = "キーワードから賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_YACHINSOUBA:
						$title = "家賃相場から賃貸を探す";
						$url = $this->_baseurl . "search/yachinsoubasearch/";
					break;
					case Apm_Config_Search::SRHKBN_CHINRYOMADORI:
						$title = "賃料×間取りから賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_JYUKYOKEITAI:
						//$title = "建物種別から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_KODAWARI:
						$title = "こだわり";
					break;
					case Apm_Config_Search::SRHKBN_TOKUSYU:
						$title = "特集";
					break;
					case Apm_Config_Search::SRHKBN_CAMPAIGN:
						$title = "キャンペーン物件から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_GAKKONAME:
						$title = "学校名から賃貸を探す";
					break;
					case Apm_Config_Search::SRHKBN_MYPAGE_ADD:
						$title = "マイページ検索条件登録";
					break;
					case Apm_Config_Search::SRHKBN_MYPAGE_UPD:
						$title = "マイページ検索条件更新";
					break;
					case Apm_Config_Search::SRHKBN_BUKENREQUEST:
						//$title = "こだわりリクエスト";
					break;
					case Apm_Config_Search::SRHKBN_SYATAKU:
						$title = "社宅物件検索";
					break;
					case Apm_Config_Search::SRHKBN_BUKENREQUEST_SYATAKU:
						$title = "社宅物件リクエスト";
					break;
					case Apm_Config_Search::SRHKBN_AREA:
						//$title = "エリアから賃貸を探す";
					break;
				}
				if ($url == "" && $this->_SrhKbn != Apm_Config_Search::SRHKBN_FREEWORD) {
					// キーワード検索は現在URLを生成するため除外
					$url = $this->_baseurl . "search/" . $this->_controller_full . "/";
				}
				$key = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . $this->_SrhKbn;
			break;

			// 都道府県
			case "PREF":
				// エリア追加
				$this->insertAreaBreadcrumb($params, $data);
				if ($this->_model_common->checkvalue($data, "PrefCd")) {
					// 都道府県パラメータが複数か
					if (count($data['PrefCd']) == 1) {
						// 都道府県を取得
						$PrefData = reset($data['PrefCd']);
						$title = $PrefData['PREF_NAME'];
						// 静的URL対応
						$url   = $this->generateStaticUrl($params, $data, $this->_pattern);
						$this->_single = true;
					} else {
						// 複数あり
						$title = "都道府県（複数選択）";
					}
				}
			break;

			case "SITEI":
				//（政令指定都市は静的URLでしか存在しないので、ここ通らないよね）
			break;

			case "CITY":
				// 政令指定都市 追加処理（都道府県CDが1件＆静的URL対応の場合）
				if (count($params['PrefCd']) == 1
					&& (   $this->_SrhKbn == Apm_Config_Search::SRHKBN_JYUSYO
						|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_JYUKYOKEITAI
						|| $this->_SrhKbn == Apm_Config_Search::SRHKBN_KODAWARI) ) {
					$this->insertSiteitosiBreadcrumb($params, $data);
				}
				if ($this->_model_common->checkvalue($data, "CityCd")) {
					// 市区町村パラメータが複数か
					if (count($data['CityCd']) == 1) {
						// 市区町村を取得
						$CityData = reset($data['CityCd']);
						$title = $CityData['CITY_NAME'];
						// 政令指定都市パンくずが存在しているか
						$sitei_bread = $this->_l_breadcrumb->getDataByKey(Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . 'SITEI');
						if (!empty($sitei_bread)) {
							// 政令指定都市名を市区町村名称からカット
							$title = mb_ereg_replace("(".$sitei_bread[0]['title'].")?", "", $title);
						}
						// 静的URL対応
						$url   = $this->generateStaticUrl($params, $data, $this->_pattern);
						$this->_single = true;
					} else {
						// 複数あり
						$title = "市区町村（複数選択）";
					}
					// 家賃相場の場合、KEYを変更する
					if ($this->_controller == "yachinsoubasearch") {
						$this->_pattern = "YACHIN_DETAIL";
						$title .= "&nbsp;家賃相場";
					}
				}
			break;

			case "TOWN":
				if ($this->_model_common->checkvalue($data, "TownCd")) {
					// 町名パラメータが複数か
					if (count($data['TownCd']) == 1) {
						// 町名を取得
						$TownData = reset($data['TownCd']);
						$title = $TownData['TOWN_NAME'];
						// 静的URL対応
						$url   = $this->generateStaticUrl($params, $data, $this->_pattern);
						$this->_single = true;
					} else {
						// 複数あり
						$title = "町名（複数選択）";
					}
				}
			break;

			case "RAIL":
				if ($this->_model_common->checkvalue($data, "RailCd")) {
					// 沿線パラメータが複数か
					if (count($data['RailCd']) == 1) {
						// 沿線を取得
						$RailData = reset($data['RailCd']);
						$title = $RailData['RAIL_NAME'];
						// 静的URL対応
						$url   = $this->generateStaticUrl($params, $data, $this->_pattern);
						$this->_single = true;
					} else {
						// 複数あり
						$title = "沿線（複数選択）";
					}
				}
			break;

			case "STCD":
				if ($this->_model_common->checkvalue($data, "StCd")) {
					// 駅パラメータが複数か
					if (count($data['StCd']) == 1) {
						// 駅を取得
						$StData = reset($data['StCd']);
						$title = $StData['ST_NAME'];
						// 静的URL対応
						$url   = $this->generateStaticUrl($params, $data, $this->_pattern);
						$this->_single = true;
					} else {
						// 複数あり
						$title = "駅（複数選択）";
					}
					// 家賃相場の場合、KEYを変更する
					if ($this->_controller == "yachinsoubasearch") {
						$this->_pattern = "YACHIN_DETAIL";
						$title .= "&nbsp;家賃相場";
					}
				}
			break;

			case "CITY_MAP":
				// 地図：市区町村コード
				if ($this->_model_common->checkvalue($data, "CityCd")) {
					$CityData = reset($data['CityCd']);
					$title = $CityData['CITY_NAME'];
				}
			break;

			case "STCODE":
				// 路線図：駅コード
				if ($this->_model_common->checkvalue($data, "stcode")) {
					if (count($data['stcode']) == 1) {
						//駅を取得
						$stcodeData = reset($data['stcode']);
						$title = $stcodeData['STCODE_NAME'];
					} else {
						$title = "路線図駅選択";
					}
					// 家賃相場の場合、KEYを変更する
					if ($this->_controller == "yachinsoubasearch") {
						$this->_pattern = "YACHIN_DETAIL";
						$title .= "&nbsp;家賃相場";
					}
				}
			break;

			case "STCD_SOBA":
				// 家賃相場（路線図から）の場合
				if ($this->_model_common->checkvalue($data, "StCd")) {
					$StData = reset($data['StCd']);
					$title = $StData['ST_NAME'] . "&nbsp;家賃相場";
					$url   = "";
					$key   = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . "STCODE";
				}
			break;

			case "TGTSTNM":
				// 通勤・通学時間：駅名入力
				$title = "通勤・通学時間条件入力";
			break;

			case "STWORD":
				// 駅名：駅名入力
				$title = "住みたい駅名入力";
			break;

			case "SCHCD":
				// 学校名：学校コード
				if ($this->_model_common->checkvalue($data, "sch_cd")) {
					// スペースが入っている場合があるので削除（住居形態等と判断できなくなるため）
					$SchData = mb_ereg_replace(" ","",mb_ereg_replace("　", "", $data['sch_cd']));
					$title = $SchData;
				}
			break;

			case "SCHNAME":
				// 学校名：学校名入力
				$title = "学校名選択";
			break;

		}

		if ($url == "") {
			if (substr($this->_controller_full, -1) == '/') {
				$controller = $this->_controller_full;
			} else {
				$controller = $this->_controller_full . '/';
			}
			// パラメータをGET化し、現在URLを生成
			if ($this->_controller == "rstobi") {
				$url = $this->_baseurl . "rstobi/" . $this->_controller . '/' . $this->generateUrlParameter($params);
			}else if ($this->_controller == "requestsearch"){
				// パターンによってURLを分岐
				if($this->_pattern=="PREF"){
					if ($params['SrhType']=="1") {
						$url = $this->_baseurl . "search/" . $this->_controller ."/city/" . $this->generateUrlParameter($params);
					}else if ($params['SrhType']=="2") {
						$url = $this->_baseurl . "search/" . $this->_controller ."/ensen/" . $this->generateUrlParameter($params);
					} else{
						$url = $this->_baseurl . "search/" . $this->_controller . '/' . $this->generateUrlParameter($params);
					}
				}else if($this->_pattern=="CITY"){
					$url = $this->_baseurl . "search/" . $this->_controller ."/jyoken/" . $this->generateUrlParameter($params);
				}else if($this->_pattern=="RAIL"){
					$url = $this->_baseurl . "search/" . $this->_controller ."/eki/" . $this->generateUrlParameter($params);
				}else if($this->_pattern=="STCD"){
					$url = $this->_baseurl . "search/" . $this->_controller ."/jyoken/" . $this->generateUrlParameter($params);
				}else{
					$url = $this->_baseurl . "search/" . $this->_controller . '/' . $this->generateUrlParameter($params);
				}
			}else {
				$url = $this->_baseurl . "search/" . $controller . $this->generateUrlParameter($params);
			}
		}

		if ($key == "") {
			$key = Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . $this->_pattern;
		}

		// パンくず配列を生成
		$pankuzu['title'] = $title;
		$pankuzu['url']   = $url;
		$pankuzu['key']   = $key;
		return $pankuzu;
	}

	/*
	 *  パンくず追加（エリア）
	 */
	private function insertAreaBreadcrumb(&$params, &$data)
	{
		// エリアデータ取得
		if (count($data['AreaCd']) == 1) {
			$AreaData = reset($data['AreaCd']);
			if ($AreaData['AREA_ROMAN'] != 'hokkaido') {
				$title = &$AreaData['AREA_NAME'];
				$url   =  $this->generateStaticUrl($params, $data, 'AREA');
				$key   =  Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . "AREA";
				$this->setBreadcrumb($title, $url, $key);
			}
		}
	}

	/*
	 *  パンくず追加（政令指定都市）
	 */
	private function insertSiteitosiBreadcrumb(&$params, &$data)
	{
		// 政令指定都市データ取得
		if (count($data['SiteitosiCd']) == 1) {
			$SiteiData = reset($data['SiteitosiCd']);
			$title = &$SiteiData['SITEI_NAME'];
			$url   =  $this->generateStaticUrl($params, $data, 'SITEI');
			$key   =  Apm_Bukkenbreadcrumb::SRH_BREAD_KBN . "SITEI";
			$this->setBreadcrumb($title, $url, $key);
		}
	}

	/*
	 *  パラメータURL生成
	 */
	public function generateUrlParameter($params)
	{
		$str = "";

		foreach ($params as $key => $val) {
			// 物件一覧のLOCKパラメータが渡ってくる事があるため、除外する
			if ($key == "chkLoad") { continue; }
			// パラメータが配列の場合
			if (is_array($val)) {
				foreach ($val as $val_key => $val_val) {
					// URL文字列へ変換
					$str .= "&" . $key . "[]=" . urlencode(mb_convert_encoding($val_val, 'UTF-8', 'SJIS'));
				}
			}
			else {
				// URL文字列へ変換
				$str .= "&" . $key . "=" . urlencode(mb_convert_encoding($val, 'UTF-8', 'SJIS'));
			}
		}
		// 先頭1文字を削除
		if ($str != "") {
			$str = "?" . substr($str, 1);
		}

		return $str;
	}

	/*
	 *  静的URL化
	 */
	private function generateStaticUrl(&$params, &$data, $pattern)
	{
		$url = "";
		$srh_params = $params;

		// 静的URLパターン抽出
		$urlPattern = "";
		$id_str     = "";
		if (!empty($params['TatemonoTypeCd']) && !empty($params['KodawariId'])) {
			$urlPattern = "x5";
			$id_str     = $params['TatemonoTypeCd'] . "-" . $params['KodawariId'];
		}
		else if (!empty($params['TatemonoTypeCd'])) {
			// 2014.06.23 パンくずリスト改善
			//$urlPattern = "x2";
			//$id_str     = $params['TatemonoTypeCd'];
		}
		else if (!empty($params['KodawariId'])) {
			$urlPattern = "x4";
			$id_str     = $params['KodawariId'];
		}

		switch ($this->_SrhKbn)
		{
			case Apm_Config_Search::SRHKBN_JYUSYO:
			case Apm_Config_Search::SRHKBN_JYUKYOKEITAI:
			case Apm_Config_Search::SRHKBN_KODAWARI:

				// パラメータ個数をチェック
				$pref_cnt = count($params['PrefCd']);
				$city_cnt = count($params['CityCd']);
				$town_cnt = count($params['TownCd']);

				// コード
				$area_cd = "";
				if (count($data['AreaCd']) == 1) {
					$DataRs  = reset($data['AreaCd']);
					$area_cd = $DataRs['AREA_ROMAN'];
					$srh_params['AreaName'] = $area_cd;
				}
				$pref_cd = (!empty($params['PrefCd']) && $pref_cnt == 1) ? Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$params['PrefCd'][0]] : "";
				$srh_params['PrefName'] = $pref_cd;

				if (empty($urlPattern)) {
					$urlPattern = 'chiiki';
				}

				switch ($pattern)
				{
					case "AREA":
						if (empty($area_cd)) break;
						$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '1');
					break;

					case "PREF":
						if (empty($area_cd) || empty($pref_cd)) break;
						if ($urlPattern == 'chiiki') {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '1.5');
						} else {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '2');
						}
					break;

					case "SITEI":
						if (empty($area_cd) || empty($pref_cd)) break;
						$sitei_cd = "";
						if (count($data['SiteitosiCd']) == 1) {
							$DataRs   = reset($data['SiteitosiCd']);
							$sitei_cd = $DataRs['SITEI_ROMAN'];
							$srh_params['SiteitosiName'] = $sitei_cd;
						}
						if (empty($sitei_cd)) break;
						$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '2.5');
					break;

					case "CITY":
						if (empty($area_cd) || empty($pref_cd) || empty($params['CityCd'])) break;
						if ($city_cnt != 1) break;
						$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '3');
					break;

					case "TOWN":
						if (empty($area_cd) || empty($pref_cd) || empty($params['TownCd'])) break;
						if ($town_cnt != 1) break;
						$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '4');
					break;
				}

			break;

			case Apm_Config_Search::SRHKBN_ENSENEKI:

				// パラメータ個数をチェック
				$pref_cnt = count($params['PrefCd']);
				$rail_cnt = count($params['RailCd']);
				$st_cnt   = count($params['StCd']);

				// コード
				$area_cd = "";
				if (count($data['AreaCd']) == 1) {
					$DataRs  = reset($data['AreaCd']);
					$area_cd = $DataRs['AREA_ROMAN'];
					$srh_params['AreaName'] = $area_cd;
				}
				$pref_cd = (!empty($params['PrefCd']) && $pref_cnt == 1) ? Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$params['PrefCd'][0]] : "";
				$srh_params['PrefName'] = $pref_cd;

				if (empty($urlPattern)) {
					$urlPattern = 'station';
				}

				switch ($pattern)
				{
					case "AREA":
						if (empty($area_cd)) break;
						$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, 'chiiki', '1');
					break;

					case "PREF":
						if (empty($pref_cd)) break;
						if ($urlPattern == 'station') {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, 'chiiki', '1.5');
							//$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '1');
						} else {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '2');
						}
					break;

					case "RAIL":
						if (empty($pref_cd)) break;
						if ($urlPattern == 'station' && !empty($params['RailCd']) && $pref_cnt == 1 && $rail_cnt == 1) {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '2.5');
						}
					break;

					case "STCD":
						if (empty($pref_cd) || empty($params['StCd'])) break;
						if ($st_cnt != 1) break;
						if ($urlPattern == 'station') {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern, '3');
						} else {
							$url = Apm_Bukkenurl::generateUrlByParameters($srh_params, $urlPattern.'s', '3');
						}
					break;
				}

			break;
		}

		return $url;
	}

	/*
	 *  編集パターンをチェック
	 */
	private function getPattern(&$params)
	{
		$Pattern = "NULL";

		switch ($this->_SrhKbn)
		{
			// 住所検索
			case Apm_Config_Search::SRHKBN_JYUSYO:
			case Apm_Config_Search::SRHKBN_JYUKYOKEITAI:
			case Apm_Config_Search::SRHKBN_KODAWARI:
			case Apm_Config_Search::SRHKBN_TOKUSYU:
			case Apm_Config_Search::SRHKBN_CAMPAIGN:
			case Apm_Config_Search::SRHKBN_CHIZU:

				// 町CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'TownCd')) {
					$Pattern = "TOWN";
				}
				// 市区町村CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'CityCd')) {
					$Pattern = "CITY";
				}
				// 政令指定都市CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'SiteitosiCd')) {
					$Pattern = "SITEI";
				}
				// 都道府県CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'PrefCd')) {
					$Pattern = "PREF";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;

			// 沿線・駅検索
			case Apm_Config_Search::SRHKBN_ENSENEKI:

				// 駅CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'StCd')) {
					$Pattern = "STCD";
				}
				// 沿線CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'RailCd')) {
					$Pattern = "RAIL";
				}
				// 都道府県CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'PrefCd')) {
					$Pattern = "PREF";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;

			// 住所／沿線検索
			case Apm_Config_Search::SRHKBN_AREA:
			case Apm_Config_Search::SRHKBN_YACHINSOUBA:
			case Apm_Config_Search::SRHKBN_CHINRYOMADORI:
			case Apm_Config_Search::SRHKBN_MYPAGE_ADD:
			case Apm_Config_Search::SRHKBN_MYPAGE_UPD:
			case Apm_Config_Search::SRHKBN_BUKENREQUEST:
			case Apm_Config_Search::SRHKBN_SYATAKU:
			case Apm_Config_Search::SRHKBN_BUKENREQUEST_SYATAKU:

				// 家賃相場（路線図から）の場合
				if ($this->_SrhKbn == Apm_Config_Search::SRHKBN_YACHINSOUBA
						&& $this->_model_common->checkvalue($params, 'r_eki_cd')) {
					$Pattern = "STCD_SOBA";
				}
				// 駅CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'StCd')) {
					$Pattern = "STCD";
				}
				// 沿線CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'RailCd')) {
					$Pattern = "RAIL";
				}
				// 町CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'TownCd')) {
					$Pattern = "TOWN";
				}
				// 市区町村CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'CityCd')) {
					$Pattern = "CITY";
				}
				// 政令指定都市CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'SiteitosiCd')) {
					$Pattern = "SITEI";
				}
				// 都道府県CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'PrefCd')) {
					$Pattern = "PREF";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;
/*
			// 地図検索
			case Apm_Config_Search::SRHKBN_CHIZU:

				// 市区町村CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'CityCd')) {
					$Pattern = "CITY_MAP";
				}
				// 都道府県CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'PrefCd')) {
					$Pattern = "PREF";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;
*/
			// 路線図検索
			case Apm_Config_Search::SRHKBN_ROSENZU:

				// 路線図駅CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'stcode')) {
					$Pattern = "STCODE";
				}
				// 都道府県CDが存在する場合
				else if ($this->_model_common->checkvalue($params, 'PrefCd')) {
					$Pattern = "PREF";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;

			// 通勤・通学時間検索
			case Apm_Config_Search::SRHKBN_TUKINTUGAKU:

				// 沿線CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'RailCd')) {
					$Pattern = "RAIL";
				}
				// 入力駅名が存在する場合
				else if ($this->_model_common->checkvalue($params, 'TgtStNm')) {
					$Pattern = "TGTSTNM";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;

			// 駅名検索
			case Apm_Config_Search::SRHKBN_SUMITAIEKI:

				// 駅CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'StCd')) {
					$Pattern = "STCD";
				}
				// 入力駅名が存在する場合
				else if ($this->_model_common->checkvalue($params, 'StWord')) {
					$Pattern = "STWORD";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;

			// 学校名検索
			case Apm_Config_Search::SRHKBN_GAKKONAME:

				// 学校CDが存在する場合
				if ($this->_model_common->checkvalue($params, 'sch_cd')) {
					$Pattern = "SCHCD";
				}
				// 入力学校名が存在する場合
				else if ($this->_model_common->checkvalue($params, 'sch_name')) {
					$Pattern = "SCHNAME";
				}
				// 全てない場合
				else {
					$Pattern = "NULL";
				}

			break;
		}

		return $Pattern;
	}

	/*
	 *  コントローラーから検索導線を取得
	 */
	private function getSrhKbn()
	{
		$SrhKbn = "";
		switch ($this->_controller)
		{
			case "jyusyosearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_JYUSYO;
			break;
			case "ensenekisearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_ENSENEKI;
			break;
			case "chizusearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_CHIZU;
			break;
			case "rosenzusearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_ROSENZU;
			break;
			case "tsukintsugakujikansearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_TUKINTUGAKU;
			break;
			case "ekinamesearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_SUMITAIEKI;
			break;
			case "yachinsoubasearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_YACHINSOUBA;
			break;
			case "chinryomadorisearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_CHINRYOMADORI;
			break;
			case "jyukyokeitaisearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_JYUKYOKEITAI;
			break;
			case "kodawari":
			case "kodawarisearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_KODAWARI;
			break;
			case "tokusyu":
			case "tokusyusearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_TOKUSYU;
			break;
			case "campaign":
			case "campaignsearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_CAMPAIGN;
			break;
			case "gakkosearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_GAKKONAME;
			break;
			case "mypagesearchadd":
				$SrhKbn = Apm_Config_Search::SRHKBN_MYPAGE_ADD;
			break;
			case "mypagesearchupd":
				$SrhKbn = Apm_Config_Search::SRHKBN_MYPAGE_UPD;
			break;
			case "requestsearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_BUKENREQUEST;
			break;
			case "houzinsearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_SYATAKU;
			break;
			case "houzinrequestsearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_BUKENREQUEST_SYATAKU;
			break;
			case "area":
			case "areasearch":
				$SrhKbn = Apm_Config_Search::SRHKBN_AREA;
			break;

		}
		return $SrhKbn;
	}

}
?>
